<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/30
 * Time: 14:26
 */
namespace app\api\controller;
use think\Db;
class Index extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $useridddd = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($useridddd,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    //banner
    public function banner(){
        $where['banner_status'] = 1;
        $res =  Db::table('up_banner')->where($where)->order('banner_sort desc')->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //热门城市
    public function hot(){
        $where['level'] = 2;
        $where['hot'] = 1;
        $res =  Db::table('up_area')->where($where)->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //比赛
    public function match(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['match_status'] = 1;
        $res = Db::table('up_match')->where($where)
            ->limit($num*$limit,$num)
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //详情
    public function detail(){
        $id = input('param.id');
        $where['match_id'] = $id;
        $res = Db::table('up_match')->where($where)
            ->find();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //热门老师
     public function teacher(){
         $page = input('param.page')?input('param.page'):1;
         $subject_id = input('param.subject_id');
         $num = 10;
         $limit = $page-1;
         $where['user_subject'] = $subject_id;
         $where['user_type'] = 2;
         $where['user_status'] = 2;
         $res = Db::table('up_u_user')->where($where)
             ->field('user_image,user_online,user_intro,user_sheng,user_shi,user_grade,user_gender,
             user_id,user_username,user_good_at,user_tcahe_style')
             ->limit($num*$limit,$num)
             ->order('user_is_top asc, user_sort desc')
             ->select();
         foreach ($res as $key=>&$vo){
             //地区
             $address = Db::table('up_area')->where('district_id',$vo['user_shi'])->value('district');
             //星级
             $star = $this->teacherStar($vo['user_id']);
             //年级
             $grade = Db::table('up_grade')->where('grade_id',$vo['user_grade'])->value('grade_name');
             // 风格
             $style = Db::table('up_teach_style')
                 ->where('style_id', $vo['user_tcahe_style'])
                 ->value('style_name');
             $vo['teacher_style'] = $style;
             $vo['address'] = $address;
             $vo['star'] = $star;
             $vo['grade'] = $grade;
         }
         return json(['status' => 200, 'msg' => $res]);
     }

     //搜索热门字
    public function hotKeyword(){
        $res = Db::table('up_hot')->order('hot_sort desc')->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //搜索
    public function keyword(){
        $type = input('param.type')?input('param.type'):1;
        $page = input('param.page')?input('param.page'):1;
        $keyword = input('param.keyword');
        $grade = input('param.grade');
        $city = input('param.city');
        $num = 10;
        $limit = $page-1;
        if($type==1){//老师
            if($grade!=''){
            $where['user_grade'] = $grade;
            }
            if($city!=''){
                $where['user_shi'] = $city;
            }
            $where['user_type'] = 2;
            $where['user_status'] = 2;
            $where['user_username|userid|district'] = array('like',"%$keyword%");
            $res = Db::table('up_u_user')->alias('u')
                ->join('up_area a','u.user_shi=a.district_id','left')
                ->where($where)
                ->field('user_image,user_online,user_intro,user_sheng,user_shi,user_grade,
                user_gender,user_id,user_username,user_tcahe_style,user_good_at')
                ->limit($num*$limit,$num)
                ->order('user_is_top asc, user_sort desc')
                ->select();
            foreach ($res as $key=>&$vo){
                //地区
                $address = Db::table('up_area')->where('district_id',$vo['user_shi'])->value('district');
                //星级
                $star = $this->teacherStar($vo['user_id']);
                //年级
                $grade = Db::table('up_grade')->where('grade_id',$vo['user_grade'])->value('grade_name');
                // 风格
                $style = Db::table('up_teach_style')
                    ->where('style_id', $vo['user_tcahe_style'])
                    ->value('style_name');
                $vo['teacher_style'] = $style;
                $vo['address'] = $address;
                $vo['star'] = $star;
                $vo['grade'] = $grade;
            }
        }elseif ($type==2){//课程
            if($grade!=''){
                $map['public_grade'] = $grade;
            }
            $map['public_name'] = array('like',"%$keyword%");
            $res = Db::table('up_public_class')->where($map)
                ->limit($num*$limit,$num)
                ->order('public_is_top asc, public_time desc')
                ->select();
            foreach ($res as $key=>&$vo){
                $count = Db::table('up_public_apply')->where('apply_public_id',$vo['public_id'])->count();
                $vo['num'] = $count;
            }
        }
        return json(['status' => 200, 'msg' => $res]);
    }
}